/**
 * 
 */
package com.ztesoft.web.baseconfig.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ztesoft.core.common.Page;
import com.ztesoft.core.convert.IArgConversionService;
import com.ztesoft.core.idproduce.ISequenceGenerator;
import com.ztesoft.framework.exception.BaseAppException;
import com.ztesoft.framework.log.ZTEsoftLogManager;
import com.ztesoft.framework.util.Utils;
import com.ztesoft.web.baseconfig.db.arg.TargetColMapArg;
import com.ztesoft.web.baseconfig.db.dao.TargetColMapDao;
import com.ztesoft.web.baseconfig.db.po.ColumnNamePO;
import com.ztesoft.web.baseconfig.db.po.ColumnsRelationPO;
import com.ztesoft.web.baseconfig.db.po.DmTargetTablePO;
import com.ztesoft.web.baseconfig.db.po.TargetColMapPO;
import com.ztesoft.web.baseconfig.service.ITargetColMapService;
import com.ztesoft.web.common.DMSConstant;
import com.ztesoft.web.common.DMSConstant.Status;

/**
 * <Description> <br>
 * 
 * @author codeCreater<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2014年11月11日 <br>
 * @since V1.0<br>
 * @see com.ztesoft.web.baseconfig.service.impl <br>
 */

@Service("targetColMapService")
public class TargetColMapServiceImpl implements ITargetColMapService {

	private static final ZTEsoftLogManager logger = ZTEsoftLogManager.getLogger(TargetColMapServiceImpl.class);

	@Autowired
	private TargetColMapDao targetColMapDao;

	/**
	 * 查询条件转换成Arg类的服务接口
	 */
	@Resource(name = "defaultArgConversionService")
	private IArgConversionService argConversionService;

	/**
	 * 主键生成器
	 */
	@Resource(name = "sequenceProcGenerator")
	private ISequenceGenerator sequenceGenerator;

	@Override
	public List<ColumnsRelationPO> selectByPrimaryKey(Integer key) throws BaseAppException {
		List<ColumnsRelationPO> crPOList = new ArrayList<ColumnsRelationPO>();
		List<TargetColMapPO> tcmPOList = targetColMapDao.selectByPrimaryKey(key);
		for (TargetColMapPO tcmPO : tcmPOList) {
			ColumnsRelationPO crPO = new ColumnsRelationPO();
			if (DMSConstant.SOURCE_COL_FLAG_F.equals(tcmPO.getSourceColFlag())) {// 不需要归档
				crPO.setSrc(tcmPO.getTargetColumn());
				crPO.setSrcValue(tcmPO.getTargetColumn());
				crPO.setTarget(tcmPO.getTargetColVal());
				crPO.setTargetValue(tcmPO.getTargetColVal());
				crPO.setRef(tcmPO.getTargetColumn() + "," + tcmPO.getTargetColVal());
				crPO.setType("INPUT_");
			} else {
				crPO.setSrc(tcmPO.getSourceColumn());
				crPO.setSrcValue(tcmPO.getSourceColumn());
				crPO.setTarget(tcmPO.getTargetColumn());
				crPO.setTargetValue(tcmPO.getTargetColumn());
				crPO.setRef(tcmPO.getSourceColumn() + "," + tcmPO.getTargetColumn());
				crPO.setType("NORMAL_");
			}
			crPOList.add(crPO);
		}
		return crPOList;
	}

	@Override
	public List<TargetColMapPO> selectByArg(TargetColMapPO record) throws BaseAppException {
		logger.debug("selectByArg begin...record={0}", record);

		// 第一种方式：自己创建arg，自行设置查询条件及操作符
		// TargetColMapArg arg = new TargetColMapArg();
		// TargetColMapCriteria criteria = arg.createCriteria();

		// 第二种方式：利用arg转换服务，转换出arg，带上查询条件及操作符，
		// 转换后，还可以自行对arg进行设置修改
		TargetColMapArg arg = argConversionService.invokeArg(TargetColMapArg.class, record);

		// ///////
		// TODO 根据业务场景，设置查询条件，示例
		// if (Utils.notEmpty(record.getUserName())) {
		// criteria.andUserNameLike(record.getUserName());
		// }
		// ///////

		return targetColMapDao.selectByArg(arg);
	}

	@Override
	public Page<TargetColMapPO> selectByArgAndPage(TargetColMapPO record, Page<TargetColMapPO> resultPage)
			throws BaseAppException {
		logger.debug("selectByArgAndPage begin...record={0}", record);

		// 第一种方式：自己创建arg，自行设置查询条件及操作符
		// TargetColMapArg arg = new TargetColMapArg();
		// //TODO 根据业务场景，设置查询条件，示例
		// TargetColMapCriteria criteria = arg.createCriteria();
		// if (Utils.notEmpty(record.getUserName())) {
		// criteria.andUserNameLike(record.getUserName());
		// }

		// 第二种方式：利用arg转换服务，转换出arg，带上查询条件及操作符，
		// 转换后，还可以自行对arg进行设置修改
		TargetColMapArg arg = argConversionService.invokeArg(TargetColMapArg.class, record);

		resultPage = targetColMapDao.selectByArgAndPage(arg, resultPage);

		return resultPage;
	}

	@Override
	public TargetColMapPO add(TargetColMapPO record) throws BaseAppException {
		logger.debug("add begin...record={0}", record);
		/*
		 * 替换，数据库必填，前台空值，的字段的值 所有非空字段： 不需要替换： 需要替换：
		 */
		// if (Utils.isEmpty(record.getIndexAnalyseFlag())) {
		// record.setIndexAnalyseFlag(DMSConstant.DONT_ANALYSE);
		// }
		// 通过前台传回来的ID，从数据库里找，是否已经有这条数据了
		// Date nowDate = new Date();
		// DmTargetTablePO dttPO =
		// dmTargetTableDao.selectByPrimaryKey(record.getTargetDataId());
		// if (Utils.isEmpty(dttPO)) {// 不存在这条记录，说明是新增
		// dmTargetTableDao.insertSelective(record);
		// } else {
		// if (dttPO.getState().equals(Status.DRAFT.toString())) {// 草稿箱，点击提交
		// // 草稿箱状态为00D或者0DA，直接修改草稿箱，状态改为00A
		// dmTargetTableDao.updateByPrimaryKeySelective(record);
		// } else {// 修改，点击提交
		// // 先把原来的记录状态改成00X
		// dttPO.setState(Status.CANCELLED.toString());
		// dttPO.setStateDate(nowDate);
		// dmTargetTableDao.updateByPrimaryKeySelective(dttPO);
		// // 再新增一条序号为 +1 的记录
		// record.setSeqNbr(dttPO.getSeqNbr() + 1);
		// record.setCreateDate(nowDate);
		// dmTargetTableDao.insertSelective(record);
		// }
		// }
		return record;
	}

	@Override
	public int update(TargetColMapPO record) throws BaseAppException {
		logger.debug("update begin...record={0}", record);

		// ///////
		// TODO 根据业务场景，进行重名校验、数据有效性校验、设置属性默认值等

		// ///////

		return targetColMapDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public int delete(TargetColMapPO record) throws BaseAppException {
		logger.debug("delete begin...record={0}", record);
		return targetColMapDao.updateTo00X(record.getTargetDataId());
	}

}
